package com.rxyb.security.controller;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author YJH
 * @version 1.0
 * @description
 * @date 2020/5/15  17:51
 */
@RestController
public class UserController {


    @GetMapping(value = "/user1")
    public Object user1(Principal principal) {
        return principal;
    }


    @GetMapping(value = "user2")
    public Object user2(Authentication authentication) {
        return authentication;
    }

    @GetMapping(value = "user3")
    public Object user3(HttpServletRequest request) {
        return request.getUserPrincipal();
    }

    @GetMapping(value = "/user4")
    public Object user4() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @Secured("ROLE_ONE")
    @GetMapping(value = "/one")
    public String one(){
        return "one";
    }

    @Secured("ROLE_TWO")
    @GetMapping(value = "/two")
    public String two(){
        return "two";
    }

    @Secured("ROLE_THREE")
    @GetMapping(value = "/three")
    public String three(){
        return "three";
    }
}
